import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../foundation/decoration/border_decoration.dart';

/// FileName border_button
///
/// @Author lax
/// @Date 2023/3/29
///
/// @Description

class BorderButton extends StatelessWidget {
  static Color? defaultColor;

  static Color? defaultUnableColor;

  static TextStyle? defaultTextStyle;

  static TextStyle? defaultUnableTextStyle;

  static EdgeInsets? defaultPadding =
      const EdgeInsets.symmetric(horizontal: 16, vertical: 8);

  final VoidCallback? onPressed;

  final String? text;

  late final TextStyle? textStyle;
  late final TextStyle? unableTextStyle;

  final double? fontSize;

  final bool enable;

  final EdgeInsets? margin;
  late final EdgeInsets? padding;

  late final Color? color;
  late final Color? unableColor;
  final Color? borderColor;

  final double? width;

  final double? height;

  final double? radius;

  final Widget? child;

  final IconData? iconData;

  BorderButton({
    super.key,
    this.onPressed,
    this.text,
    TextStyle? textStyle,
    TextStyle? unableTextStyle,
    this.fontSize,
    this.enable = true,
    this.margin,
    EdgeInsets? padding,
    Color? color,
    Color? unableColor,
    this.borderColor,
    this.width,
    this.height,
    this.radius,
    this.child,
    this.iconData,
  }) {
    this.color = color ?? defaultColor;
    this.unableColor = unableColor ?? defaultUnableColor;
    this.textStyle = textStyle ??
        defaultTextStyle ??
        TextStyle(
          fontSize: fontSize ?? 14,
          fontWeight: FontWeight.normal,
          color: borderColor ?? PublicColor.primary,
        );
    this.unableTextStyle = unableTextStyle ??
        defaultUnableTextStyle ??
        TextStyle(
          fontSize: fontSize ?? 14,
          fontWeight: FontWeight.normal,
          color: PublicColor.lightText,
        );
    this.padding = padding ?? defaultPadding;
  }

  @override
  Widget build(BuildContext context) {
    return onPressed == null
        ? body(context)
        : GestureDetector(
            onTap: () {
              if (enable != true) {
                return;
              }
              if (onPressed != null) {
                onPressed!();
              }
            },
            child: body(context),
          );
  }

  Widget body(BuildContext context) {
    final style = enable ? textStyle : unableTextStyle;

    final hei = boundingTextSize(context, text ?? '', style).height;

    var pad = padding ?? EdgeInsets.zero;
    var rad = radius;
    if (rad == null) {
      if (height != null) {
        rad = height! / 2;
      } else {
        rad = (hei + pad.top + pad.bottom) / 2;
      }
    }

    if (width != null && height != null) {
      pad = EdgeInsets.zero;
    } else {
      if (width != null) {
        pad = EdgeInsets.only(top: pad.top, bottom: pad.bottom);
      }
      if (height != null) {
        pad = EdgeInsets.only(left: pad.left, right: pad.right);
      }
    }

    late Widget widget;
    if (iconData == null) {
      widget = child ?? Text(text ?? '', style: style);
    } else {
      widget = Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 2, right: 2),
            child: Icon(
              iconData,
              color: style?.color,
              size: (style?.fontSize ?? 14) + 2,
            ),
          ),
          child ?? Text(text ?? '', style: style)
        ],
      );
    }

    return Container(
      width: width,
      height: height,
      margin: margin,
      padding: pad,
      alignment: Alignment.center,
      decoration: BorderDecoration(
        radius: rad,
        color: enable ? color : unableColor,
        borderColor: borderColor ?? style?.color,
      ),
      child: widget,
    );
  }

  /// 计算文本大小
  static Size boundingTextSize(
      BuildContext context, String text, TextStyle? style,
      {int maxLines = 2 ^ 31, double maxWidth = double.infinity}) {
    if (style == null || text.isEmpty) {
      return Size.zero;
    }
    final TextPainter textPainter = TextPainter(
      textDirection: TextDirection.ltr,
      locale: Localizations.localeOf(context),
      text: TextSpan(text: text, style: style),
      maxLines: maxLines,
    )..layout(maxWidth: maxWidth);
    return textPainter.size;
  }
}
